package edu.task.repository;

import edu.task.model.SysRole;
import edu.task.model.SysTask;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysTaskRepository extends JpaRepository<SysTask, Long> {

    final String JOIN_PROJECT_TASK=" left join sys_project_task pt on pt.task_id = t.task_id ";
    final String JOIN_PROJECT=" left join sys_project p on p.project_id = pt.project_id ";

//    final String JOIN_USER_ROLE=" left join sys_msg ut on ut.task_id = t.task_id ";
//    final String JOIN_USER=" left join sys_user u on u.user_id = ut.user_id ";

    @Modifying
    @Query(value = " delete from sys_task where task_id=:taskId ", nativeQuery = true)
    int deleteTaskById(Long taskId);

    @Query(value = "select t.task_id from sys_task t " +JOIN_PROJECT_TASK+JOIN_PROJECT+
            "WHERE p.project_id=:projectId",nativeQuery = true)
    List<Long> selectTaskListByProjectId(Long projectId);

    @Query(value = "select ut.task_id from sys_user_task ut " +
            "left join sys_user u on u.user_id = ut.user_id " +
            "left join sys_task t on t.task_id = ut.task_id " +
            "WHERE u.user_id=:userId",nativeQuery = true)
    List<Long> selectTaskListByUserId(Long userId);

    @Query(value = "select * from sys_task WHERE status = '0'" , nativeQuery = true)
    List<SysTask> selectTaskListByStatus();
}
